﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 提交管理平台V1
{
    public partial class PrintListFrm : Form
    {
        下厂验清单数据集 ds;
        string strListReportFileName = Application.StartupPath + "\\template\\下厂验清单.rpt";
        DataTable dtPrinterSettings;

        private void iniPrinterSettings()
        {
            if (dtPrinterSettings == null)
            {
                dtPrinterSettings = new DataTable();
                dtPrinterSettings.Columns.Add("模板");
                dtPrinterSettings.Columns.Add("指定打印机");
            }
            else
            {
                dtPrinterSettings.Rows.Clear();
            }
            if (Properties.Settings.Default.PrinterSettings.Length > 0)
            {
                string[] tmpSettings = Properties.Settings.Default.PrinterSettings.Split(';');
                if (tmpSettings.Length % 2 != 0)
                {
                    MessageBox.Show("打印机设置有误，请检查后重新设置！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
                DataRow r;
                for (int i = 0; i < tmpSettings.Length - 1; i += 2)
                {
                    r = dtPrinterSettings.NewRow();
                    r["模板"] = tmpSettings[i];
                    r["指定打印机"] = tmpSettings[i + 1];
                    dtPrinterSettings.Rows.Add(r);
                }
            }
        }

        private int GetSettingIndex(string strTempletName)
        {
            int ret = -1;
            for (int i = 0; i < dtPrinterSettings.Rows.Count; i++)
            {
                if (dtPrinterSettings.Rows[i]["模板"].ToString() == strTempletName)
                {
                    ret = i;
                    break;
                }
            }
            return ret;
        }

        public PrintListFrm(DataGridViewSelectedRowCollection dgvsrc)
        {
            InitializeComponent();
            GetReportData(dgvsrc);
        }

        private void PrintListFrm_Load(object sender, EventArgs e)
        {
            iniPrinterSettings();
        }

        private void GetReportData(DataGridViewSelectedRowCollection dgvsrc)
        {
            ds = new 下厂验清单数据集();
            DataRow r;
            try
            {
                for (int i = dgvsrc .Count -1; i >= 0; i--)
                {
                    r = ds.Tables["ReportData"].NewRow();
                    r["提交时间"] = Convert.ToDateTime ( dgvsrc[i].Cells["提交时间"].Value.ToString ()).ToString ("yy.M.d");
                    r["合同号"] = dgvsrc[i].Cells["合同号"].Value;
                    r["型号规格"] = dgvsrc[i].Cells["型号"].Value.ToString() + "-" + dgvsrc[i].Cells["尺寸"].Value.ToString() + "-" + dgvsrc[i].Cells["特性"].Value.ToString() + "-" + dgvsrc[i].Cells["电压"].Value.ToString() + "-" + dgvsrc[i].Cells["容量"].Value.ToString();
                    r["技术标准"] = dgvsrc[i].Cells["执行标准"].Value;
                    r["生产批号(B)"] = dgvsrc[i].Cells["批号"].Value;
                    r["生产批号(A)"] = dgvsrc[i].Cells["芯片号"].Value;
                    r["合同数量"] = dgvsrc[i].Cells["合同数量"].Value;
                    r["已验数量"] = string.Empty;
                    r["提交数量"] = dgvsrc[i].Cells["提交数量"].Value;
                    r["筛选数量"] = dgvsrc[i].Cells["筛选数量"].Value;
                    r["淘汰率"] = dgvsrc[i].Cells["淘汰率"].Value;
                    r["生产日期"] = Convert .ToDateTime (dgvsrc[i].Cells["生产日期"].Value.ToString ()).ToString ("yy.M.d");
                    r["试验数量"] = dgvsrc[i].Cells["试验数量"].Value;
                    r["DPA数量"] = dgvsrc[i].Cells["DPA数量"].Value;
                    r["验收方式"] = dgvsrc[i].Cells["验收方式"].Value;
                    ds.Tables["ReportData"].Rows.Add(r);
                }
                r = ds.Tables["OtherData"].NewRow();
                ds.Tables["OtherData"].Rows.Add(r);
                dataGridView1.DataSource = ds.Tables["Reportdata"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ds.Tables["OtherData"].Rows[0]["报表标题"] = textBox1.Text;
            reportpreviewFrm newreportpreviewfrm = new reportpreviewFrm(ds,strListReportFileName );
            newreportpreviewfrm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ds.Tables["OtherData"].Rows[0]["报表标题"] = textBox1.Text;
            CrystalDecisions.CrystalReports.Engine.ReportDocument Rdoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            FileInfo fi = new FileInfo(strListReportFileName);
            Rdoc.Load(fi.FullName  );
            int ind = GetSettingIndex(fi.Name.Substring(0, fi.Name.Length - 4));
            if (ind != -1)
            {
                Rdoc.PrintOptions.PrinterName = dtPrinterSettings.Rows[ind]["指定打印机"].ToString();
            }
            Rdoc.SetDataSource(ds);
            Rdoc.Refresh();
            while (!Rdoc.IsLoaded)
            { }
            try
            {
                Rdoc.PrintToPrinter( (int)numericUpDown1.Value , false, 0, 0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}